package com.malethan.seemorej.crud;

import java.util.regex.Pattern;

/**
 * <p>Copied from Matt Raible's AppFuse Maven plugin</p>
 * <p>This class contains methods than can be used in Freemarker templates for String manipulation.</p>
 */
public class StringUtils {
    //----------------------------------------------------------------------- Static Properties and Constants

    private static final Pattern ES = Pattern.compile("^.*(sh|ss|ch|o|i)$");
    private static final Pattern ICES = Pattern.compile("^.*(ex|ix)$");
    private static final Pattern NOT_VOWEL_Y = Pattern.compile("^.*[^aeiou]y$");

    //----------------------------------------------------------------------- Static Methods
    //----------------------------------------------------------------------- Instance Properties
    //----------------------------------------------------------------------- Constructors
    //----------------------------------------------------------------------- Getters and Setters
    //----------------------------------------------------------------------- Instance Methods

    public String capitaliseFirstLetter(String str) {
        return str.substring(0, 1).toUpperCase() + str.substring(1);
    }


    public String humaniseCamelCase(String word) {
        return new HumaniseCamelCase().humanise(word);
    }


    public String getPluralForWord(final String word) {
        String plural = null;

        if (StringUtils.isNotBlank(word)) {
            plural = word + "s";

            if (ES.matcher(word).matches()) {
                plural = word + "es";
            } else if (NOT_VOWEL_Y.matcher(word).matches()) {
                String stripY = word.substring(0, word.length() - 1);
                plural = stripY + "ies";
            } else if (ICES.matcher(word).matches()) {
                String strip_X = word.substring(0, word.length() - 2);
                plural = strip_X + "ices";
            }
        }

        return plural;
    }

    public String getLowerCaseFirstLetter(final String word) {
        return word.substring(0,1).toLowerCase() + word.substring(1);
    }

    public String getUpperCaseFirstLetter(final String word) {
        return word.substring(0,1).toUpperCase() + word.substring(1);
    }

    private static boolean isNotBlank(String word) {
        return word != null && word.trim().length() > 0;
    }
}
